--[[

    Copyright (c) 2011-2020 baby-bus.com

    TODO:   SpriteName
    Author: DemonYao
    Date:   2020.09.21
 
    http://www.babybus.com/ 

]] 




--------------------------
-- 引入命名空间
--------------------------
-- local BaseLayer = import("app.common.layer.BaseLayer")




--[[
    类
--]]


local M = classSprite("dragonBone")
local dbFactory = dragonBones.CCFactory



--------------------------
-- 构造函数
--------------------------

function M:ctor(params)
    -- [超类调用]
    M.super.ctor(self, params)
    -- [本类调用]
    self.dragonBonesDataFile  = params.dragonBonesDataFile      -- 骨骼文件
    self.dragonBonesTexFile   = params.dragonBonesTexFile       -- 骨骼文件
    self.armatureName         = params.armatureName             -- 骨骼名称
    self.animName             = params.animName                 -- 初始骨骼动作名称（选填）
    self.index = params.index
end

--------------------------
-- 渲染
--------------------------

function M:onRender()
    M.super.onRender(self)
    -- [本类调用]
    -- 加载骨骼数据
    self:addDragonBonesData()
    -- 创建骨架
    self:createArmature()
    -- 动作事件监听
    self:bindDBEvent(self.armatureDisplay)
end

-- 加载节点
function M:loadDisplay()
    -- 
end

-- 加载渲染以外的其他操作
function M:onEnterTransitionFinish()
    --触控 

    self:bindTouchLocate()
    -- tool:loadHelpRect({
	-- 	node = self,
	-- 	pos = cc.p(0, 0),
	-- 	size = cc.size(self:cw(), self:ch()),
	-- 	anchorPoint = cc.p(0, 0)
	-- }):opacity(200):z(-1)
end

-- 加载骨骼数据
function M:addDragonBonesData()
    if self.dragonBonesDataFile and self.dragonBonesTexFile then
	    dragonBones.CCFactory:loadDragonBonesData(self.dragonBonesDataFile, "test")
        dragonBones.CCFactory:loadTextureAtlasData(self.dragonBonesTexFile, "test")
    end
end

-- 创建骨骼
function M:createArmature()
    local armatureDisplay = dragonBones.CCFactory:buildArmatureDisplay(self.armatureName)
    CCNodeExtend.extend(armatureDisplay)
    -- 设置节点大小
    self:setContentSize(cc.size(armatureDisplay:cw(), armatureDisplay:ch()))
    armatureDisplay:anchor(cc.p(0.5, 0.5))
    local size = armatureDisplay:getContentSize()
    local anchor = armatureDisplay:getAnchorPoint()
    
    armatureDisplay:to(self):p(size.width * anchor.x, size.height * anchor.y)
    self.armatureDisplay = armatureDisplay

    -- 获取Animation
    self:getAnimation()
    if self.animName then
        self:play(self.animName)
    end
end



--------------------------
-- 功能函数
--------------------------
-- 动作播放
function M:play(animationName, playTimes)
    local animation = self.animation
    if animation then
        animation = self:getAnimation()
    end
    playTimes = playTimes or -1
    print(playTimes)
    local state = animation:play(animationName, playTimes)
    return state
end

-- 获取Animation
function M:getAnimation()
    self.animation = self.armatureDisplay:getAnimation()
    return self.animation
end

-- 获取动作状态
function M:getAnimationState(animationName)
    local state = self:getAnimation():getState(animationName)
    return state
end

-- 淡入播放指定的动画。
function M:fadeInplay(animationName, fadeInTime, params)
    params = params or {}
    local playTimes   = params.playTimes or -1
    local layer       = params.layer or 0
    local group       = params.group or "" 
    local fadeOutMode = params.fadeOutMode or 0
    local state = self:getAnimation():fadeIn(animationName, fadeInTime, playTimes, layer, group, fadeOutMode)
    return state
end

-- 骨骼事件绑定
function M:bindDBEvent(armatureDisplay)
    armatureDisplay:addDBEventListener("start", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("loopComplete", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("complete", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("fadeIn", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("fadeInComplete", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("fadeOut", handler(self, self.animationEventHandler))
 
    armatureDisplay:addDBEventListener("fadeOutComplete", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("frameEvent", handler(self, self.animationEventHandler))

    armatureDisplay:addDBEventListener("soundEvent", handler(self, self.animationEventHandler))
end

function M:animationEventHandler(eventObject)
    if eventObject.type == "start" then

    elseif eventObject.type == "loopComplete" then
        if self.index == 1 then
            local attackState = self:getAnimationState("attack_01")
            if not attackState then
                attackState = self:fadeInplay("attack_01", 1, {playTimes = 1, layer = 1})
                attackState:setResetToPose(false)
                attackState:setAutoFadeOutTime(0.1)
                attackState:addBoneMask("chest");
                attackState:addBoneMask("effect_l");
                attackState:addBoneMask("effect_r");
            end
        elseif self.index == 2 then
            self:play("skill_03")
        end
    elseif eventObject.type == "complete" then
        if self.index == 2 then
            if eventObject.animationState:getName() == "skill_03" then
                self:fadeInplay("walk", 0.2)
            end
        end
    elseif eventObject.type == "fadeIn" then       

    elseif eventObject.type == "fadeInComplete" then

    elseif eventObject.type == "fadeOut" then

    elseif eventObject.type == "fadeOutComplete" then

    elseif eventObject.type == "frameEvent" then

    elseif eventObject.type == "soundEvent" then

    end
end




--------------------------
-- 触控
-------------------------- 

function M:onTouchBegan(x, y, touches)
    --  触控有效
    return SIGN_TOUCH_BEGAN_SWALLOWS
end

function M:onTouchMoved(x, y, touches)
    --  
end

function M:onTouchEnded(x, y, touches)
    --  
end







--------------------------
-- 属性
--------------------------




--------------------------
-- 父类重写
--------------------------




----------------------------------------------------
-- 析构
--------------------------

function M:onDestructor()
    -- [超类调用]
    M.super.onDestructor(self)
    -- [本类调用]
    self.armatureDisplay:removeDBEventListener("start")

    self.armatureDisplay:removeDBEventListener("loopComplete")

    self.armatureDisplay:removeDBEventListener("complete")

    self.armatureDisplay:removeDBEventListener("fadeIn")

    self.armatureDisplay:removeDBEventListener("fadeInComplete")

    self.armatureDisplay:removeDBEventListener("fadeOut")
 
    self.armatureDisplay:removeDBEventListener("fadeOutComplete")

    self.armatureDisplay:removeDBEventListener("frameEvent")

    self.armatureDisplay:removeDBEventListener("soundEvent")
end








return M






